package cn.cs.common;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Component
public class CommonRpc {
    @Resource
    private RedisUtils redisUtils;
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";
    static final String accessKeyId = "LTAIvqzndnfpNjyz";
    static final String accessKeySecret = "I0C8NdJCleVDOthS2yfvYYNx6gXjzT";

    /**
     * 阿里云发送验证短信码接口
     *
     */
    public String  sendBatchSms(String phone){
        if(redisUtils.exist(phone)){
            redisUtils.delete(phone);
        }
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        Random random = new Random();
        String YZCode = "";
        for (int i = 0; i < 6; i++) {
            YZCode += random.nextInt(10);
        }
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "主事丫环");
        request.putQueryParameter("TemplateCode", "SMS_163438513");
        HashMap map = new HashMap();
        map.put("code", YZCode);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            redisUtils.set(phone,120,YZCode);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return  YZCode;
    }

    /**
     * 阿里云发送短信接口(暂不能使用)
     *
     */
    public static  String senedSms(String phoneNum) throws  Exception{
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        Random random = new Random();
        String YZCode ="";
        for (int i=0;i<6;i++)
        {
            YZCode+=random.nextInt(10);
        }
        request.setMethod(MethodType.POST);
        request.setVersion("2017-05-25");
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("name","您好，您的验证码为：");
        map.put("code",YZCode);
        request.setPhoneNumbers(phoneNum);
        request.setSignName("主事丫环");
        request.setTemplateCode("SMS_163438513");
        request.setRegionId("cn-hangzhou");
        request.setTemplateParam(JSON.toJSONString(map));
        try {
            SendSmsResponse sendSmsResponse = client.getAcsResponse(request);
            System.out.println(sendSmsResponse.getCode()+"|"+sendSmsResponse.getMessage());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return YZCode;
    }

    /**
     * 阿里云发送提醒信息接口
     *
     */
    public static   String  sendAlertInfo(String text,String phone){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "主事丫环侯保师傅");
        request.putQueryParameter("TemplateCode", "SMS_172885391");
        HashMap map = new HashMap();
        map.put("time", text.replace(" ",""));
        request.putQueryParameter("TemplateParam", JSON.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return  text;
    }
}
